package com.taylor.BaiYueGu.Lanjieqi;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author Taylor
 */
@Controller
@Slf4j
@Api(tags = "上传文件api")
public class FormTestController {
    @ResponseBody
    @PostMapping("/upload")
    public String upload(@RequestParam("email") String email,
                         @RequestParam("username") String username,
                         @RequestParam("headerImg") MultipartFile headerImg,
                         @RequestParam("photo") MultipartFile[] photos
    ) throws IOException {
        log.info("上传的图片信息有：email={},username={},headerImg={},photos={}", email, username, headerImg.getSize(), photos.length);
        if (!headerImg.isEmpty()) {
            String originalFilename = headerImg.getOriginalFilename();
            headerImg.transferTo(new File("D:\\" + originalFilename));
        }
        for (MultipartFile photo : photos) {
            String originalFilename = photo.getOriginalFilename();
            photo.transferTo(new File("D:\\" + originalFilename));
        }
        return "上传成功";
    }
}
